//
//  HMNavigationController.swift
//  WB25
//
//  Created by HM on 2017/6/29.
//  Copyright © 2017年 itheima. All rights reserved.
//

import UIKit

class HMNavigationController: UINavigationController, UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 重写设置手势代理
        interactivePopGestureRecognizer?.delegate = self

    }
    // 返回
    @objc private func backClick(){
        popViewController(animated: true)
    }
    // 监听用户将要边缘手势
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        // 如果为true 意思接收手势 反之不接受
//        if childViewControllers.count == 1 {
//            return false
//        }
        return (childViewControllers.count != 1)
    }
    
    
    // 重写父类以后的方法
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {

        // 判断count 大于0 (一级界面去掉)
        if childViewControllers.count > 0 {
            // 设置默认值
            var title = "返回"
            // 二级界面 显示是上一个界面的title 其它的是显示返回
            if childViewControllers.count == 1 {
                title = childViewControllers.first?.navigationItem.title ?? ""
                //隐藏tabBar
                viewController.hidesBottomBarWhenPushed = true
            }
            
            // 设置左侧按钮
            // 如果自定义了左侧导航按钮 会导致点击屏幕左侧 右滑手势失效
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(imgName: "navigationbar_back_withtext", title: title, target: self, action: #selector(backClick))
        }

        // 监听该方法完成左侧按钮设置
        super.pushViewController(viewController, animated: animated)
    }

}
